package com.redlight.pool;

import com.redlight.base.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by chao on 2019/1/23.
 */
@Component
public class ThreadPoolInit {
    private Logger logger= LoggerFactory.getLogger(getClass());
    private static final  Map<String, PoolArgs> poolArgMap = Global.poolArgMap;
    private static volatile  ThreadPoolInit threadPoolInit;
    public ThreadPoolInit(){}
    public static ThreadPoolInit getInstance() {
        if (threadPoolInit == null) {
            synchronized (ThreadPoolInit.class) {
                if (threadPoolInit == null) {
                    threadPoolInit = new ThreadPoolInit();
                }
            }
        }
        return threadPoolInit;
    }

    @Resource
    private ThreadPoolFactory threadPoolFactory;
    @Bean
    public void initRedLightPool() throws Exception {
        try {
            PoolArgs args=new PoolArgs();
            args.setId("redLight");
            ThreadPool pool = threadPoolFactory.getPool("redLight");
            if(pool==null){
                poolArgMap.put("redLight",args);
                threadPoolFactory.newPool(args);
                logger.info("初始化redLight线程池成功");
            }
        }catch (Exception e){
            logger.error("初始化redLight线程池失败。。。。");
            throw new Exception("初始化redLight线程池失败。。。。",e);
        }

    }
//    @Bean
    public Map<String, PoolArgs> getPoolArgMap(){
        return poolArgMap;
    }

}
